package com.psp.bu.controller.springmvc;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.*;
import com.psp.bu.controller.res.*;
import com.psp.bu.controller.res.bean.*;
import com.psp.bu.controller.springmvc.req.*;

/**
 * 商品-相关接口
 **/
@Controller
@RequestMapping(value = "/bu/bs", produces = "application/json")
public class SProductController {
	@Autowired
	com.psp.bu.controller.ProductController productController;

	/**
	 * 商品保存
	 **/
	@RequestMapping("/v1/save")
	@ResponseBody
	public BaseResult save(@Validated SaveProductParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		BaseResult res = new BaseResult();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return productController.save(param, request, response);
	}

	/**
	 * 商品列表
	 **/
	@RequestMapping("/v1/list")
	@ResponseBody
	public ListResult<RProductListBean> list(@Validated ProductListParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		ListResult<RProductListBean> res = new ListResult<RProductListBean>();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return productController.list(param, request, response);
	}

	/**
	 * 商品批量上架
	 **/
	@RequestMapping("/v1/onShelf")
	@ResponseBody
	public BaseResult onShelf(@Validated ProductOnShelfParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		BaseResult res = new BaseResult();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return productController.onShelf(param, request, response);
	}

	/**
	 * 商品批量下架
	 **/
	@RequestMapping("/v1/offShelf")
	@ResponseBody
	public BaseResult offShelf(@Validated ProductOffShelfParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		BaseResult res = new BaseResult();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return productController.offShelf(param, request, response);
	}

	/**
	 * 商品删除
	 **/
	@RequestMapping("/v1/delete")
	@ResponseBody
	public BaseResult delete(@Validated ProductDeleteParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		BaseResult res = new BaseResult();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return productController.delete(param, request, response);
	}

	/**
	 * 商品详情
	 **/
	@RequestMapping("/v1/detail")
	@ResponseBody
	public ObjectResult<RProductDetailBean> detail(@Validated ProductDetailParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		ObjectResult<RProductDetailBean> res = new ObjectResult<RProductDetailBean>();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return productController.detail(param, request, response);
	}

	/**
	 * 获取编辑时的商品详情
	 **/
	@RequestMapping("/v1/getProductInfo")
	@ResponseBody
	public ObjectResult<RProductEditDetailBean> getProductInfo(@Validated GetProductInfoParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		ObjectResult<RProductEditDetailBean> res = new ObjectResult<RProductEditDetailBean>();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return productController.getProductInfo(param, request, response);
	}

	/**
	 * 获取服务类型
	 **/
	@RequestMapping("/v1/getSubTypes")
	@ResponseBody
	public ObjectResult<RServicesTypeBean> getSubTypes(@Validated GetSubTypesParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		ObjectResult<RServicesTypeBean> res = new ObjectResult<RServicesTypeBean>();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return productController.getSubTypes(param, request, response);
	}

	/**
	 * 根据adcode获取下级区域
	 **/
	@RequestMapping("/v1/getAreas")
	@ResponseBody
	public ListResult<RAreaListBean> getAreas(@Validated GetAreaListParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		ListResult<RAreaListBean> res = new ListResult<RAreaListBean>();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return productController.getAreas(param, request, response);
	}

	/**
	 * 服务属性
	 **/
	@RequestMapping("/v1/getProductAttributes")
	@ResponseBody
	public ListResult<RAttributeBean> getProductAttributes(@Validated GetProductAttributesParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		ListResult<RAttributeBean> res = new ListResult<RAttributeBean>();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return productController.getProductAttributes(param, request, response);
	}

	/**
	 * 服务信息
	 **/
	@RequestMapping("/v1/getProductInfoBySkuno")
	@ResponseBody
	public ObjectResult<RProductSkuBean> getProductInfoBySkuno(@Validated GetProductInfoBySkunoParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		ObjectResult<RProductSkuBean> res = new ObjectResult<RProductSkuBean>();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return productController.getProductInfoBySkuno(param, request, response);
	}
}
